#include "replacedialog.h"
#include "ui_replacedialog.h"

ReplaceDialog::ReplaceDialog(QWidget *parent) : QDialog(parent),
                                                ui(new Ui::ReplaceDialog)
{
    ui->setupUi(this);
    setWindowFlags(Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint);
    setWindowTitle("替换");
    connect(ui->findlineEdit, &QLineEdit::textChanged, this, &ReplaceDialog::textChangeSlot);
}

ReplaceDialog::~ReplaceDialog()
{
    delete ui;
}

void ReplaceDialog::textChangeSlot()
{
    if (ui->findlineEdit->text().trimmed().isEmpty())
    {
        ui->findButton->setEnabled(false);
        ui->replaceButton->setEnabled(false);
        ui->replaceAllButton->setEnabled(false);
    }
    else
    {
        ui->findButton->setEnabled(true);
        ui->replaceButton->setEnabled(true);
        ui->replaceAllButton->setEnabled(true);
    }
}

void ReplaceDialog::on_findButton_clicked()
{
    bool checkbox1 = ui->uplowcheckBox->isChecked(); //大小写
    bool checkbox2 = ui->loopcheckBox->isChecked();  //循环
    QString value = ui->findlineEdit->text().trimmed();
    //发送自定义信号
    emit this->findtext(value, checkbox1, checkbox2);
}

void ReplaceDialog::on_replaceButton_clicked()
{
    bool checkbox1 = ui->uplowcheckBox->isChecked(); //大小写
    bool checkbox2 = ui->loopcheckBox->isChecked();  //循环
    QString target = ui->findlineEdit->text().trimmed();
    QString value = ui->replacelineEdit->text().trimmed();
    //回调事件
    emit this->replacetext(target, value, checkbox1, checkbox2, false);
}

void ReplaceDialog::on_replaceAllButton_clicked()
{
    bool checkbox1 = ui->uplowcheckBox->isChecked(); //大小写
    bool checkbox2 = ui->loopcheckBox->isChecked();  //循环
    QString target = ui->findlineEdit->text().trimmed();
    QString value = ui->replacelineEdit->text().trimmed();
    //回调事件
    emit this->replacetext(target, value, checkbox1, checkbox2, true);
}

void ReplaceDialog::on_cancelButton_clicked()
{
    this->close();
}
